
let app = getApp();
// url: app.globalData.baseUrl+ 'user.check'

Page({

    /**
     * 页面的初始数据
     */
    data: {
        region: ['河南省', '郑州市', '中原区'],
        customItem: '全部',
        phoneNumber:'',

        verificationCode:'',
        codeText: '获取验证码',  // 按钮初始文字
        isCounting: false,  // 是否正在倒计时
        countDown: 60,  // 倒计时初始秒数
        exitPhone:''
    },
    // 将 JSON 对象转换为字符串
    
    // 输入手机号
  handlePhoneInput: function (e) {
    let that = this;
    that.setData({
      phoneNumber: e.detail.value
    });
  },

  // 输入验证码
  handleCodeInput: function (e) {
    this.setData({
      verificationCode: e.detail.value
    });
  },

  // 获取验证码
  getCode: function () {
    const data = {
      phoneNumber: this.data.phoneNumber,
      verificationCode: this.data.verificationCode
    };
    
    // 将 JSON 对象转换为字符串
    const jsonData = JSON.stringify(data);
    const that = this;

    if (this.data.isCounting) return;

    wx.request({
      url:'http://localhost:8080/api/getCode',
      method:'POST',
      data:{
        phoneNumber: this.data.phoneNumber
      },
      header: {
        'Content-Type': 'application/x-www-form-urlencoded'  
      },
      success(res) {
        wx.showToast({
          title: '验证码已发送',
          icon: 'none'
        });

        // 开始倒计时
        that.startCountDown();
      }
    });
  },

  // 开始倒计时
  startCountDown: function () {
    const that = this;
    this.setData({
      isCounting: true,
      codeText: `重新获取 (${this.data.countDown}s)`
    });

    const interval = setInterval(() => {
      let count = that.data.countDown - 1;
      if (count <= 0) {
        clearInterval(interval);
        that.setData({
          isCounting: false,
          codeText: '获取验证码',
          countDown: 60
        });
      } else {
        that.setData({
          countDown: count,
          codeText: `重新获取 (${count}s)`
        });
      }
    }, 1000);
  },

  // 验证验证码并跳转
  verifyCode: function () {
    let that = this;
    wx.request({
      url: 'http://localhost:8080/user/selectByNumber',
      method:'GET',
      success(res){
        that.data.exitPhone = res.data
        console.log(that.data.exitPhone);
      }
    })
    if(that.data.exitPhone == 1){
      console.log(that.data.exitPhone);
      wx.showToast({
        title: '用户已注册',
        icon:'error'
      })
    }else{
      // 用户未注册
      const that = this;
      
      wx.request({
        url: 'http://localhost:8080/api/verifyCode',
        method: 'POST',
        data: {
          phoneNumber: this.data.phoneNumber,
          code: this.data.verificationCode
        },
        header: {
          'Content-Type': 'application/x-www-form-urlencoded'  
        },
        success(res) {
          if (res.data) {
            wx.navigateTo({
              url: `/pages/register1/register1?phoneNumber=${that.data.phoneNumber}&verificationCode=${that.data.verificationCode}` 
            });
          } else {
            wx.showToast({
              title: '验证码错误或已过期',
              icon: 'none'
            });
          }
        }
      });
    }
    
  },

    back2:function(){
      wx.navigateBack({
        delta:1
      })
    },

    onClose2:function(){
    console.log("1111"),
    // 关闭页面或者返回上一页
    wx.switchTab({

      url: '/pages/my/my',
   
    }),
    console.log("333")
  },

    bindRegionChange(e) {
        console.log('picker发送选择改变，携带值为', e.detail.value) 
        this.setData({
            region: e.detail.value
        })
    },
    /**
     * 生命周期函数--监听页面加载
     */
    onLoad(options) {

    },

    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady() {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow() {

    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide() {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload() {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh() {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom() {

    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage() {

    }
})